﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Text.RegularExpressions;
using Portal.CMS;


namespace Portal.CDSP
{
    public class FFunctionWP : WebPart
    {

        public override string WebPartId
        {
            get
            {
                return "WebSite.FFunctionWP";
            }
        }


        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string Title
        {
            get
            {
                return GetText("FTitle", "CDSP-Nhóm tin thông báo");
            }
        }


        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    
                    new ViewCls("Right",GetText("Right","Hiển thị bên phải"),1),
                    
                   };
            }
        }

        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string ClassName
        {
            get
            {
                return "FFunctionWP";
            }
        }


        public override RenderInfoCls ClientRenderWebPartWithInfo(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            RenderInfoCls oRenderInfoCls = new RenderInfoCls();

            try
            {

                if (ViewName.Equals("Right"))
                {
                    oRenderInfoCls.Content = ClientRenderWebPartRight(TabModuleId, HasPorlet, ref oRenderInfoCls.ModuleTitle);
                }

                return oRenderInfoCls;// ResourceBLL.GetText(WebPartId, "ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                oRenderInfoCls.ErrorMsg = ex.Message.ToString();
                oRenderInfoCls.Error = true;
                return oRenderInfoCls;
            }
        }

        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            return new FSpecialSubjectBLL().ReadingForServiceGroupNewsItem(belongSite, ActiveLanguage);
        }


        // Danh mục nhóm tin trang chủ
        private string ClientRenderWebPartRight(string TabModuleId, string HasPorlet, ref string ModuleTitle)
        {
            try
            {
                const int MaxString = 40;
                int TopNews;
                try
                {
                    TopNews = int.Parse(Environment.Environments.GetAppSettings("TopNews"));
                }
                catch { TopNews = 4; }

                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
                StringBuilder Html = new StringBuilder();
                //Html+= WebPartUtilities.AttachJavascript(WebPartFolder, "rssfeed.js");
                FSpecialSubjectCls FSubjectCls = new FSpecialSubjectBLL().CreateModelItem(TabModuleCls.Id, TabModuleCls.belongSite);
                string GroupSubject = FSubjectCls.GroupNewsItemName;
                string GroupNewsItemId = FSubjectCls.GroupNewsItemId;
                string belongSite = TabModuleCls.belongSite;
                DataTable dtNews = new FSpecialSubjectBLL().ReadingNews_ChuyenMuc(GroupNewsItemId, TopNews, belongSite);
                

                string NewsByCategoryUrl = Environments.GetNewsCategoryUrl(belongSite, GroupNewsItemId, GroupSubject, "_RunTime");
                               
                Html.Append(
                    "<div class=\"rightcolStyle\">" +
                    "<div class=\"rightcolHeader\">" +
                    "			<ul> \n" +
                    "				<li><a href=\"" + NewsByCategoryUrl + "\">" + GroupSubject.ToUpper() + "</a></li>\n" +
                    "				<li><img src=\"images/Trang-Chu-cut_49.png\"></li>\n" +
                    "			</ul>\n" +
                    "</div>");

                if (dtNews.Rows.Count == 0) return Html.Append("Không có dữ liệu!</div>").ToString();

                int iNews = 0;
                string ShortId = (string)dtNews.Rows[iNews]["ShortId"];
                string Subject = (string)dtNews.Rows[iNews][Environments.ActiveLanguage + "Subject"];

                string ShortContent = string.Empty;
                string LinkType = "_runtime";
                string SmallImage = string.Empty;

                DateTime EntryDate = (DateTime)dtNews.Rows[iNews]["EntryDate"];

                string Url = Environments.GetNewsUrl(belongSite, ShortId, Subject, LinkType);
                ShortContent = (string)dtNews.Rows[iNews][Environments.ActiveLanguage + "ShortContent"];
                int Count = 0;
                int j = 0;
                ShortContent = Regex.Replace(ShortContent, @"<(.|\n)*?>", string.Empty);
                int Length = ShortContent.Length;
                for (int i = 1; i < ShortContent.Length; i++)
                {
                    if (ShortContent[i - 1].ToString().Equals(" ") && !ShortContent[i].ToString().Equals(" "))
                        Count++;
                    if (Count <= MaxString)
                    {
                        j = i;
                    }
                }
                if (Count > MaxString)
                {
                    ShortContent = ShortContent.ToString().Remove(j, (Length - j));
                    ShortContent = string.Concat(ShortContent, "...");
                }                

                Html.Append(
                        "<div class=\"rightcolContent\"> " +                          
                            "<ul>");
                if (TopNews > dtNews.Rows.Count)
                {
                    TopNews = dtNews.Rows.Count;
                }               
                for (int iIndexNews = 0; iIndexNews <= TopNews-1; iIndexNews++)
                {
                    ShortId = (string)dtNews.Rows[iIndexNews]["ShortId"];
                    Subject = "";
                    try
                    {
                        Subject = (string)dtNews.Rows[iIndexNews][Environments.ActiveLanguage + "Subject"];
                    }
                    catch { }
                    ShortContent = "";
                    try
                    {
                        ShortContent = (string)dtNews.Rows[iIndexNews][Environments.ActiveLanguage + "ShortContent"];
                    }
                    catch { }                    
                    try
                    {
                        EntryDate = (DateTime)dtNews.Rows[iIndexNews]["EntryDate"];
                    }
                    catch { }                                    
                    Url = Environments.GetNewsUrl(belongSite, ShortId, Subject, LinkType);
                    ShortContent = Ultility.ClearHTMLTags(ShortContent);
                    Html.Append(
                    "<li>" +
                    "<a href=\"" + Url + "\">" + Subject + " " +
                    "</a>" +                 
                    "</li>");
                }
                Html.Append("</ul>");
                //if (FSubjectCls.AllowRss == 1)
                //{
                //    Html += "<div class = \"ShowRss\">" +
                //            "<a href=\"javascript:ClientSideCallRssChannel('" + TabModuleCls.Id + "','" + belongSite + "','" + GroupSubject + "');\">" +
                //            "<span><img src = \"images/rss.gif\" /></span>&nbsp;" +
                //            "</a></div>";
                //}
                Html.Append("</div></div>");
                return Html.ToString();
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }

        //[AjaxPro.AjaxMethod()]
        //public string ServerSideCallRssChannel(string GroupNewsId, string belongSite, string GroupSubject)
        //{
        //    try
        //    {
        //        var RSSChannel = new RSimpleSyndicationBLL().MakeXmlDataForChanel(GroupSubject, "RSS - SODC Website", "sodc.gov.vn", "SODC", "SODC", DateTime.Now, DateTime.Now, GroupNewsId, "xml", belongSite);
        //        return RSSChannel.Url;

        //    }
        //    catch (Exception ex)
        //    {
        //        return "ERROR: " + ex.Message;
        //    }
        //}

        public string ShowTooltip(string Tooltip)
        {
            string[] charsToTrim = { "'", "[", "]", "*", "\'", ";", ",", ".", "#", "@", "%", };//!@#$%^&*()+=-[]\';,./{}|\":<>?
            for (int i = 0; i < charsToTrim.Length; i++)
            {
                if (Tooltip.IndexOf(charsToTrim[i]) != -1)
                {
                    Tooltip.Replace(charsToTrim[i], "\"" + charsToTrim[i] + "\"");
                }
            }
            return Tooltip;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FFunctionWP), ActivePage);
        }

    }
}
